package com.jerry.huihome.module.find_near_mine.activity;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseActivity;
import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.find_near_mine.adapter.AfflatusItemAdapter;
import com.jerry.huihome.module.find_near_mine.entity.AfflatusItemEntity;
import com.jerry.huihome.utils.EventMessage;
import com.jerry.huihome.utils.OkHttpUtils;
import com.jerry.huihome.views.ExpandListView;
import com.jerry.huihome.views.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import de.greenrobot.event.EventBus;

public class AfflatuspictoActivity extends BaseActivity{
    @BindView(R.id.workname)
    TextView workname;
    @BindView(R.id.touxiangbig                )
    RoundImageView touxiang;
    @BindView(R.id.houseArea)
    TextView houseArea;
    @BindView(R.id.houseType)
    TextView houseType;
    @BindView(R.id.decoStyle)
    TextView decoStyle;
    @BindView(R.id.appraiseCount)
    TextView appraiseCount;
    @BindView(R.id.commentCount)
    TextView commentCount;
    @BindView(R.id.shareCount)
    TextView shareCount;
    @BindView(R.id.afflatusitem_itemList)
    ExpandListView item_itemList;

    private int id;
    private List<AfflatusItemEntity.BodyBean.DetailItemsBean> detailItemsBean;
    private AfflatusItemAdapter afflatusItemAdapter;
    private ImageLoader imageLoader;

    public AfflatuspictoActivity(){
    }

    @Override
    protected int getLayoutId(){
        return R.layout.activity_afflatuspicto;
    }

    //创建一个存放图片地址的集合
    private  ArrayList<String> imaurl = new ArrayList<>();
    @Override
    protected void initViews(){
        Intent intent = getIntent();
        //如果没有取到"status"的值，则默认返回-1.
        id = intent.getIntExtra("id", -1);
        detailItemsBean = new ArrayList<>();

        imageLoader = BaseApplication.getApplication().getImageLoader();
        afflatusItemAdapter = new AfflatusItemAdapter(this, detailItemsBean);
        item_itemList.setAdapter(afflatusItemAdapter);
        //setListViewHeight();
        item_itemList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                //传递集合需要序列化,这里我们改用eventbus
                Intent intent = new Intent();
                //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //intent.putExtra("listIma",detailItemsBean);
                intent.setClass(AfflatuspictoActivity.this,ImageActivity.class);
                intent.putStringArrayListExtra("url",imaurl);
                intent.putExtra("position",position);
               // imaurl.add(itemEntity.getBody().getDetailItems().get(position).getListImg());
                //此处EventMessage为other目录下类,非utils中
                startActivity(intent);
                //要先跳转页面,在post数据
               // EventBus.getDefault().post(new EventMessage(detailItemsBean));
            }
        });


        //分享========
        ShareSDK.initSDK(this);
    }
    @OnClick(R.id.affatus_share)
    public void share(View view){

        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
    }
    @OnClick(R.id.goback)
    public void goback(View view){finish();}

    private void setListViewHeight(){
        int totalHeight=0;
        for(int i = 0;i<afflatusItemAdapter.getCount();i++){
            afflatusItemAdapter.getView(i,null,item_itemList);
            item_itemList.measure(1,1);
            totalHeight +=item_itemList.getMeasuredHeight();
        }
        ViewGroup.LayoutParams layoutParams = item_itemList.getLayoutParams();
        layoutParams.height = totalHeight +(item_itemList.getDividerHeight()*(item_itemList.getCount()-1));
        item_itemList.setLayoutParams(layoutParams);
    }

    private AfflatusItemEntity itemEntity;

    private String reqUrl ;

    @Override
    protected void loadData(){
        reqUrl = Urls.AFFLATUS_item_URL + id;
        OkHttpUtils.get(reqUrl, new OkHttpUtils.OnGetDataListener(){


            @Override
            public void onResponse(String url, String data){
                if(data!=null){

                    Gson gson = new Gson();
                    itemEntity = gson.fromJson(data, AfflatusItemEntity.class);
                    final AfflatusItemEntity.BodyBean body = itemEntity.getBody();
                    detailItemsBean.addAll(body.getDetailItems());
                    int size = detailItemsBean.size();
                    for(int i = 0 ; i < size;i++){
                        String listImgUrl = detailItemsBean.get(i).getListImg();
                        imaurl.add(listImgUrl);
                    }
                    //此处EventMessage为other目录下类,非utils中
                    //EventBus.getDefault().post(new EventMessage(detailItemsBean));
                    imageLoader.displayImage(body.getAvatar(),touxiang);

                    //头像点击事件,跳转页面
                    touxiang.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v){
                          // EventBus.getDefault().post(new EventMessage());
                            /*Intent intent = new Intent(AfflatuspictoActivity.this, DesignerDetailActivity.class);
                            intent.putExtra("designerId", body.getDesignerid());
                            startActivity(intent);*/
                            EventBus.getDefault().post(new EventMessage("designerid",body.getDesignerid()+""));

                        }
                    });
                    workname.setText(body.getWorkname());
                    houseArea.setText(body.getHouseType() + "");
                    houseType.setText(body.getHouseType());
                    decoStyle.setText(body.getDecoStyle());
                    appraiseCount.setText(body.getAppraiseCount() + "");
                    commentCount.setText(body.getCommentCount() + "");
                    shareCount.setText(body.getShareCount() + "");
                    afflatusItemAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(String url, String error){
            }
        });
    }
}
